package com.appistry.maven.plugin.far.remote.ssh;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

/**
 * <code>Sftp</code> class executes a secure FTP action.
 * 
 * <p>
 * The actual action/command is performed/executed by the
 * {@link com.appistry.maven.plugin.far.remote.ssh.SshFtp SshFtp} handler. This
 * approach keeps all the sftp action-specific stuff in the handler class.
 * 
 * <p>
 * When used with the {@link com.appistry.maven.plugin.far.remote.ssh.Command
 * Command} pattern, this class serves as the receiver/target object.
 * 
 */
public class Sftp implements RemoteOperation {

	/**
	 * File transfer operation type. Currently only put and get operations are
	 * supported
	 * 
	 */
	public enum TYPE {
		put, get
	}

	private String srcFilename;
	private String destFilename;
	private Sftp.TYPE transferType;
	private SshConfig sshConfig;
	private SshFtp delegate;

	public Sftp() {
		super();
	}

	public String getDestFilename() {
		return destFilename;
	}

	public void setDestFilename(String destFilename) {
		this.destFilename = destFilename;
	}

	public Sftp.TYPE getTransferType() {
		return transferType;
	}

	public void setTransferType(Sftp.TYPE transferType) {
		this.transferType = transferType;
	}

	public String getSrcFilename() {
		return srcFilename;
	}

	public void setSrcFilename(String srcFilename) {
		this.srcFilename = srcFilename;
	}

	public SshConfig getSshConfig() {
		return sshConfig;
	}

	public void setSshConfig(SshConfig sshConfig) {
		this.sshConfig = sshConfig;
	}

	public JSchWrapper getDelegate() {
		return delegate;
	}

	/**
	 * Executes the FTP operation
	 * 
	 * @throws JSchException
	 * @throws SftpException
	 */
	public void run() throws JSchException, SftpException {
		// would it be a better idea to construct this in the constructor of
		// this class or perhaps injected via a setter?
		delegate = new SshFtp(sshConfig);

		delegate.executeCommand(srcFilename, destFilename, getTransferType()
				.name());

		// if (TYPE.put.name().equalsIgnoreCase(transferType.name())) {
		// System.out.println("About to call SshFtp.execute");
		// delegate.executeCommand(srcFilename, destFilename);
		// } else if (TYPE.get.name().equalsIgnoreCase(transferType.name())) {
		// delegate.executeCommand(srcFilename, destFilename);
		// }
	}

}
